<?php
namespace Admin\Controller;
use Think\Controller;
class StudentController extends BaseControlle {
    public function __construct()
    {
        parent::__construct();
    }
    public function index(){
        //$this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>[ 您现在访问的是Home模块的Index控制器 ]</div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
        $this->display('index');
    }

    //添加学生前端UI
    public function NewStudentUI(){

        $grade      = M("grade");
        $ret_grade  = $grade->order('number')->select();

        $this->assign('grade',$ret_grade);
        $this->display();
    }

    //添加学生
    public function NewStudent(){
        if(IS_POST){
            $name       = I("post.name");
            $mobile     = I("post.mobile");
            $grade      = I("post.grade");
            $address   = I("post.address");
            $district   = I("post.district");
            $school     = I("post.school");
            if(!$name || !$mobile || !$grade){
                $this->ajaxReturn(array('code'=>500,'msg'=>'请按要求提交数据'));
            }

            $db = array(
                'mobile'=> $mobile,
                'name'=>$name,
                'grade'=>$grade,
                'district'=>$district,
                'address'=>$address,
                'school'=>$school
            );

            $student_information     = M('student_information');
            $student_login           = M('student_login');
            $ret_student_login = $student_login->where("mobile='$mobile'")->find();
            if($ret_student_login){
                $this->ajaxReturn(array('code'=>400,'msg'=>'手机号已存在!'));
            }


            $ret_student_information = $student_information->data($db)->add();
            $user_uid                = $student_information->where("mobile='$mobile'")->find();

            //$student_login     = M("student_login");
            $prefix = GeneratePassword(10);
            $lodin_db = array(
                'uid'      =>$user_uid['uid'],
                'mobile'  =>$mobile,
                'prefix'  =>$prefix,
                'password'=>UserPasswordEncrypt($mobile,$prefix)
            );
            $ret_student_login =$student_login->data($lodin_db)->add();

            if($ret_student_login AND $ret_student_information){
                $this->ajaxReturn(array('code'=>200));
            }elseif(!$ret_student_information){
                $this->ajaxReturn(array('code'=>501,'msg'=>'用户信息添加失败!！'));
            }elseif (!$ret_student_login){
                $this->ajaxReturn(array('code'=>502,'msg'=>'用户登陆信息添加失败！'));
            }else{
                $this->ajaxReturn(array('code'=>503,'msg'=>'未知添加失败'));
            }

        }else{
            $this->ajaxReturn(array('code'=>502,'msg'=>'提交非法数据！'));
        }
    }


    //编辑学生信息前端UI
    public function EditStudentUI(){
        $uid = I("get.uid");
        $student_region = array();//用户地区记录
        $DB_PREFIX = C('DB_PREFIX');
        if(!$uid) exit('请输入用户UID');
        $student_information = M('student_information');
        $grade               = M("grade");
        $region               = M("region");
        $ret_grade = $grade->order('number')->select();
        $ret_student_information = $student_information
            ->join("__STUDENT_LOGIN__ ON __STUDENT_INFORMATION__.uid=__STUDENT_LOGIN__.uid",'LEFT')
            ->join("__GRADE__ ON __GRADE__.id=__STUDENT_INFORMATION__.grade",'LEFT')
            ->field("{$DB_PREFIX}student_information.*,{$DB_PREFIX}student_login.mobile,{$DB_PREFIX}grade.name as gradename")
            ->where("{$DB_PREFIX}student_information.uid='$uid'")
            ->find();

        $student_region[3] = $region->where(array('REGION_ID'=>$ret_student_information['region']))->find();//地区
        $student_region[2] = $region->where(array('REGION_ID'=>$student_region[3]['PARENT_ID']))->find();//城市
        $student_region[1] = $region->where(array('REGION_ID'=>$student_region[2]['PARENT_ID']))->find();//省、直辖市

        $region_ret[1] = $region->where(array('PARENT_ID'=>1))->select();//所有省
        $region_ret[2] = $region->where(array('PARENT_ID'=>$student_region[2]['PARENT_ID']))->select();//查询省下面的所有市
        $region_ret[3] = $region->where(array('PARENT_ID'=>$student_region[3]['PARENT_ID']))->select();//查询省下面的所有区

        $this->assign('user',$ret_student_information);
        $this->assign('student_region',$student_region);
        $this->assign('region_ret',$region_ret);
        $this->assign('grade',$ret_grade);
        $this->display();
    }

    /**
     *编辑学生数据
     */
    public function EditStudent(){
        if(IS_POST){
            $uid        = I("post.uid");
            $name       = I("post.name");
            $mobile     = I("post.mobile");
            $password   = I("post.password");
            $grade      = I("post.grade");
            $district   = I("post.district");
            $address   = I("post.address");
            $school     = I("post.school");
            if(!$name || !$mobile || !$grade){
                $this->ajaxReturn(array('code'=>500,'msg'=>'请按要求提交数据'));
            }

            $student_information = M('student_information');
            $student_login       = M('student_login');

            $ret_student_login       = $student_login->where("uid='$uid'")->find();
            $ret_student_information = $student_information->where("uid='$uid'")->find();

            if(!$ret_student_information){
                $this->ajaxReturn(array('code'=>500,'msg'=>'该用户不存在'));
            }

            $student_information_db = array(
                'name'=>$name,
                'grade'=>$grade,
                'region'=>$district,
                'district'=>$address,
                'school'=>$school
            );
            $save_student_information = $student_information->where("uid='$uid'")->save($student_information_db);
            if ($save_student_information === false){
                $this->ajaxReturn(array('code'=>500,'msg'=>'用户主信息修改失败！'));
            }
            //检测手机号重复
            $is_mobile_student_login = $student_login->where("mobile='$mobile' AND uid!='$uid'")->find();
            if ($is_mobile_student_login){
                $this->ajaxReturn(array('code'=>500,'msg'=>'该手机号已存在！'));
            }

            $student_login_db = array(
                'uid'=>$uid,
                'mobile'=>$mobile
            );
            $prefix = GeneratePassword(10);
            if(!$ret_student_login){
                //如果加入了更改密码，则使用新密码，否则使用手机号作为初始化密码
                $student_login_db['prefix'] = $prefix;
                if($password){
                    $student_login_db['password'] = UserPasswordEncrypt($password,$prefix);
                }else{
                    $student_login_db['password'] = UserPasswordEncrypt($mobile,$prefix);
                }
                $save_student_login = $student_login->where("uid='$uid'")->add($student_login_db);
                if ($save_student_login === false){
                    $this->ajaxReturn(array('code'=>500,'msg'=>'学生登录信息修改失败！'));
                }
            }else{
                $student_login_db['prefix'] = $prefix;
                if($password){
                    $student_login_db['password'] = UserPasswordEncrypt($password,$prefix);
                }else{
                    $student_login_db['password'] = UserPasswordEncrypt($mobile,$prefix);
                }
                $save_student_login = $student_login->where("uid='$uid'")->save($student_login_db);
                if ($save_student_login === false){
                    $this->ajaxReturn(array('code'=>500,'msg'=>'学生登录信息修改失败！'));
                }
            }

            //修改成功
            $this->ajaxReturn(array('code'=>200));
        }

    }

    //删除学生信息
    public function DeleteStudent(){
        if(IS_POST){
            $uid = I("post.uid",0,'intval');
            $student_information = M('student_information');
            $student_login       = M('student_login');
            if ($uid<1){
                $this->ajaxReturn(array('code'=>500,'msg'=>'请提交正确的学生信息'));
            }

            $ret_student_login       = $student_login      ->where(array('uid'=>$uid))->delete();
            $ret_student_information = $student_information->where(array('uid'=>$uid))->delete();

            if(!$ret_student_information && !$ret_student_login){
                $this->ajaxReturn(array('code'=>502,'msg'=>'删除失败!'));
            }else{
                $this->ajaxReturn(array('code'=>200));
            }
        }else{
            $this->ajaxReturn(array('code'=>501,'msg'=>'请按照规范提交!'));
        }
    }

    public function Manage(){
        $student_information = M('student_information');

        $page   =      I('get.page',0,'intval');
        $count  =      $student_information->count();
        $page = DBPage($count,30,$page);
        $DB_PREFIX = C('DB_PREFIX');

        $student_list = $student_information
            ->join("__STUDENT_LOGIN__ ON __STUDENT_INFORMATION__.uid=__STUDENT_LOGIN__.uid",'LEFT')
            ->join("__GRADE__ ON __GRADE__.id=__STUDENT_INFORMATION__.grade",'LEFT')
            ->field("{$DB_PREFIX}student_information.*,{$DB_PREFIX}student_login.mobile,{$DB_PREFIX}grade.name as gradename")
            ->limit(30)->page($page['page'])->select();
        //$student_list = $student_login->limit(30)->page($page['page'])->select();
        $this->assign('student_list',$student_list);
        $this->assign('count',$page['countPage']);
        $this->assign('page',$page['page']);
        $this->display();
    }

    //查询地区信息
    public function Region(){
        $regionid = I("post.regionid",0,'intval');
        if ($regionid == 0){
            $this->ajaxReturn(array('code'=>500,'msg'=>'查询数据不正确'));
        }

        $region     = M("region");
        $ret_region = $region->where(array('PARENT_ID'=>$regionid))->select();
        if($ret_region){
            $this->ajaxReturn(array('code'=>200,'data'=>$ret_region));
        }else{
            $this->ajaxReturn(array('code'=>500,'msg'=>'查询失败'));
        }

    }
}